package com.framemodule.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.framemodule.BaseApp.BaseApp;
import com.framemodule.R;
import com.framemodule.utils.EventListener4FragmentControlActivity;
import com.framemodule.utils.Logger;
import com.kymjs.frame.view.IDelegate;

import java.util.ArrayList;

import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import cn.bingoogolapple.refreshlayout.BGARefreshViewHolder;

/**
 * Created by User on 2015/12/14.
 */
public abstract class BaseFragmentPresenter<T extends IDelegate> extends FragmentPresenter<T> {
    protected String token;//用于对比判断是否登陆状态有变化

    protected ProgressDialog mLoadingDialog;
    protected OnFragmentCloseListener mOnFragmentCloseListener;
    protected EventListener4FragmentControlActivity mFragmentControlActivity;
    private int resultCode;
    private Object resultObj;


    public void setOnFragmentCloseListener(OnFragmentCloseListener onFragmentCloseListener) {
        mOnFragmentCloseListener = onFragmentCloseListener;
    }

    public void setFragmentControlActivity(EventListener4FragmentControlActivity fragmentControlActivity) {
        mFragmentControlActivity = fragmentControlActivity;
    }

    @Nullable

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        initModel();

        return view;
    }


    public EventListener4FragmentControlActivity getFragmentControlActivity() {
        return mFragmentControlActivity;
    }

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (BaseApp.getSharedInstance() != null) {
//            token = BaseApp.getSharedInstance().loadString("token");
//        }

        mLoadingDialog = new ProgressDialog(BaseApp.getInstance());
        mLoadingDialog.setCanceledOnTouchOutside(false);

        Logger.e(this.getClass().getSimpleName());
    }

    @Override
    public void onResume() {
        super.onResume();
        //TODO 统计等
//        MobclickAgent.onResume(getActivity());
    }

    @Override
    public void onPause() {
        super.onPause();
        //TODO 统计等
//        MobclickAgent.onPause(getActivity());
    }

    @Override
    public void onDestroy() {
        new Thread(new Runnable() {
            @Override
            public void run() {
//                OkHttpClientManager.getInstance().cancelTag(BaseApp.getInstance());//BaseApp.getInstance()
            }
        }).start();
        super.onDestroy();
    }

    /**
     * startActivity
     *
     * @param clazz
     */
    protected void readyGo(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
    }

    /**
     * startActivity with bundle
     *
     * @param clazz
     * @param bundle
     */
    protected void readyGo(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * startActivityForResult
     *
     * @param clazz
     * @param requestCode
     */
    protected void readyGoForResult(Class<?> clazz, int requestCode) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivityForResult(intent, requestCode);
    }

    /**
     * startActivityForResult with bundle
     *
     * @param clazz
     * @param requestCode
     * @param bundle
     */
    protected void readyGoForResult(Class<?> clazz, int requestCode, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * startActivity then finish
     *
     * @param clazz
     */
    protected void readyGoThenKill(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        if (!"LoadActivity".equals(clazz.getSimpleName())) {
            if (getActivity() != null) {
                getActivity().finish();
            }
        } else {
            if (token.equals("")) {

            } else {
                intent.putExtra("LOOK", "LOOK");
//                BaseApp.getSharedInstance().clear();
            }
        }
        startActivity(intent);
    }

    /**
     * 进入的时候展示引导图，只在第一次的进入的时候展示
     *
     * @param name           //引导图展示的
     * @param imageSourceIds //引导图的source id
     */
    public void showGuide(String name, int... imageSourceIds) {
        if (imageSourceIds == null) {
            return;
        }
        if (imageSourceIds.length == 0) {
            return;
        }

        boolean isFirstIn = BaseApp.getMySharedInstance().loadBoolean(name, true);
        if (isFirstIn) {
            ArrayList<Integer> imageGuides = new ArrayList<>();
            for (int imageSourceId : imageSourceIds) {
                imageGuides.add(imageSourceId);
            }
            Bundle bundle = new Bundle();
            bundle.putIntegerArrayList("imageGuides", imageGuides);
//            readyGo(GuideActivity.class, bundle);
            BaseApp.getMySharedInstance().saveBoolean(name, false);
        }

    }

    /**
     * 初始化model对象
     */
    protected abstract void initModel();

    public void initRefreshLayout(BGARefreshLayout mRefreshLayout, BGARefreshLayout.BGARefreshLayoutDelegate delegate) {
        // 为BGARefreshLayout 设置代理
        mRefreshLayout.setDelegate(delegate);
        // 设置下拉刷新和上拉加载更多的风格     参数1：应用程序上下文，参数2：是否具有上拉加载更多功能
        BGARefreshViewHolder refreshViewHolder = new BGANormalRefreshViewHolder(getActivity(), true);
        // 设置下拉刷新和上拉加载更多的风格
        mRefreshLayout.setRefreshViewHolder(refreshViewHolder);


        // 为了增加下拉刷新头部和加载更多的通用性，提供了以下可选配置选项  -------------START
        // 设置正在加载更多时不显示加载更多控件
        // mRefreshLayout.setIsShowLoadingMoreView(false);
        // 设置正在加载更多时的文本
        refreshViewHolder.setLoadingMoreText("加载更多");
        // 设置整个加载更多控件的背景颜色资源 id
        refreshViewHolder.setLoadMoreBackgroundColorRes(R.color.gray);
        // 设置整个加载更多控件的背景 drawable 资源 id
        //refreshViewHolder.setLoadMoreBackgroundDrawableRes(loadMoreBackgroundDrawableRes);
        // 设置下拉刷新控件的背景颜色资源 id
        //refreshViewHolder.setRefreshViewBackgroundColorRes(refreshViewBackgroundColorRes);
        // 设置下拉刷新控件的背景 drawable 资源 id
        //refreshViewHolder.setRefreshViewBackgroundDrawableRes(refreshViewBackgroundDrawableRes);
        // 设置自定义头部视图（也可以不用设置）     参数1：自定义头部视图（例如广告位）， 参数2：上拉加载更多是否可用
        //mRefreshLayout.setCustomHeaderView(mBanner, false);
        // 可选配置  -------------END
    }

    public void initRefreshLayout(BGARefreshLayout mRefreshLayout, BGARefreshLayout.BGARefreshLayoutDelegate delegate,View customHeaderView) {
        // 为BGARefreshLayout 设置代理
        mRefreshLayout.setDelegate(delegate);
        // 设置下拉刷新和上拉加载更多的风格     参数1：应用程序上下文，参数2：是否具有上拉加载更多功能
        BGARefreshViewHolder refreshViewHolder = new BGANormalRefreshViewHolder(getActivity(), true);
        // 设置下拉刷新和上拉加载更多的风格
        mRefreshLayout.setRefreshViewHolder(refreshViewHolder);
        // 为了增加下拉刷新头部和加载更多的通用性，提供了以下可选配置选项  -------------START
        // 设置正在加载更多时不显示加载更多控件
        // mRefreshLayout.setIsShowLoadingMoreView(false);
        // 设置正在加载更多时的文本
        refreshViewHolder.setLoadingMoreText("加载更多");
        // 设置整个加载更多控件的背景颜色资源 id
        refreshViewHolder.setLoadMoreBackgroundColorRes(R.color.gray);
        // 设置整个加载更多控件的背景 drawable 资源 id
        //refreshViewHolder.setLoadMoreBackgroundDrawableRes(loadMoreBackgroundDrawableRes);
        // 设置下拉刷新控件的背景颜色资源 id
        //refreshViewHolder.setRefreshViewBackgroundColorRes(refreshViewBackgroundColorRes);
        // 设置下拉刷新控件的背景 drawable 资源 id
        //refreshViewHolder.setRefreshViewBackgroundDrawableRes(refreshViewBackgroundDrawableRes);
        // 设置自定义头部视图（也可以不用设置）     参数1：自定义头部视图（例如广告位）， 参数2：上拉加载更多是否可用
        mRefreshLayout.setCustomHeaderView(customHeaderView, true);
        // 可选配置  -------------END
    }
    protected  void setFragmentResult(int resultCode,Object resultObj){
        this.resultCode = resultCode;
        this.resultObj = resultObj;
    }


    public interface OnFragmentCloseListener {
        void onFragmentClose(int resultCode,Object obj);
    }

    @Override
    public void onDetach() {
        if (mOnFragmentCloseListener!=null){
            mOnFragmentCloseListener.onFragmentClose(resultCode,resultObj);
        }
        super.onDetach();
    }
}
